package bajw;

import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.imageio.ImageIO;

import org.junit.Test;

import com.sbl.bajw.util.HttpUtil;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class text1 {
	
	public void testfile() throws IOException {
		
		String targetImagePath = "d:/t.jpg";
		String resultImagePath = "d:/t1.jpg";
	        
	 	ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        BufferedImage bufferedImage = ImageIO.read(new File(targetImagePath));
        JPEGEncodeParam parm = JPEGCodec.getDefaultJPEGEncodeParam(bufferedImage);
        parm.setQuality(0.05f, false);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(byteArrayOutputStream, parm);  
        encoder.encode(bufferedImage); 
        
        FileOutputStream resultOut = new FileOutputStream(resultImagePath);
        resultOut.write(byteArrayOutputStream.toByteArray());
        resultOut.flush();
        resultOut.close();
	}
	
	@Test
	public void testBaidugps(){
		String apkKey = "X1j8IIXvLlbWg1eP3K9jdmww";
		String lon = "113.918264";
		String lat = "22.567471";
		String url = "http://api.map.baidu.com/geocoder/v2/?ak="+apkKey+"&location="+lat+","+lon+"&output=json&pois=0";
		
		String json =HttpUtil.getData(url, null, "UTF-8");
		System.out.println(json);
	}
	
	
	public void face(){
		 String str="http://183.62.134.180:35180/frfacework";
	        String filePath="D:\\testface\\6.jpg";
	        try {
	            URL url=new URL(str);
	            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
	            connection.setDoInput(true);
	            connection.setDoOutput(true);
	            connection.setRequestMethod("POST");
	            connection.setRequestProperty("content-type", "application/octet-stream");
	            BufferedOutputStream  out=new BufferedOutputStream(connection.getOutputStream());
	            
	            //读取文件上传到服务器
	            File file=new File(filePath);
	            FileInputStream fileInputStream=new FileInputStream(file);
	            byte[]bytes=new byte[1024];
	            int numReadByte;
	            while((numReadByte=fileInputStream.read(bytes,0,1024))>0)
	            {
	                out.write(bytes, 0, numReadByte);
	            }

	            out.flush();
	            fileInputStream.close();
	            DataInputStream in=new DataInputStream(connection.getInputStream());
	            InputStreamReader ireader = new InputStreamReader(in);
	            BufferedReader reader = new BufferedReader(ireader);
	            
	            String result = "";
	            String line = "";
	            while(line != null){
	            	result += line;
	            	line = reader.readLine();
	            }
	            System.out.println(result);
	            
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	}
}
